from django.shortcuts import render
from django_redis import get_redis_connection
from rest_framework.viewsets import ViewSet
from rest_framework.generics import ListAPIView,RetrieveAPIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView
# Create your views here.
from luffyapi.apps.course.pagenations import StandardPageNumberPagination
from . import models
from .serializers import CourseCategoryModelSerializer, CourseModelSerializer, CourseDetailModelSerializer, CourseChapterModelSerializer
from django.conf import settings


class CategoryView(ListAPIView):
	queryset = models.CourseCategory.objects.filter(is_deleted=False,is_show=True)
	serializer_class = CourseCategoryModelSerializer



class CourseView(ListAPIView):
	queryset = models.Course.objects.filter(is_deleted=False,is_show=True).order_by('id')
	serializer_class = CourseModelSerializer
	filter_fields = ('course_category',)
	pagination_class = StandardPageNumberPagination

class CourseDetailView(RetrieveAPIView):
	queryset = models.Course.objects.filter(is_deleted=False,is_show=True)
	serializer_class = CourseDetailModelSerializer


from django_filters.rest_framework import DjangoFilterBackend
class ChapterView(ListAPIView):
	queryset = models.CourseChapter.objects.filter(is_show=True,is_deleted=False)
	serializer_class = CourseChapterModelSerializer
	filter_backends = [DjangoFilterBackend,]
	filter_fields = ('course',)

from luffyapi.libs.polyv import PolyvPlayer
from rest_framework.permissions import IsAuthenticated

class PolyvView(APIView):
	permission_classes = [IsAuthenticated]
	def get(self,request):
		polyv_obj = PolyvPlayer(settings.POLYV_CONF['userid'],settings.POLYV_CONF['secretKey'],settings.POLYV_CONF['tokenUrl'])
		vid = request.query_params.get('vid')
		viewerIp = request.META.get('REMOTE_ADDR')
		viewerId = request.user.id
		viewerName = request.user.username

		token_dict = polyv_obj.get_video_token(vid,viewerIp,viewerId,viewerName)

		return Response(token_dict)


